package com.cxg.fairy.ec.pay.status;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.util.log.LatteLogger;
import com.cxg.fairy.util.toasty.Toasty;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;
import com.joanzapata.iconify.widget.IconTextView;
import com.mylhyl.superdialog.SuperDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/15.
 */

public class PayStatusDelegate extends LatteDelegate implements ISuccess{

    @BindView(R2.id.iv_pay_status_img)
    IconTextView payStatusImg = null;

    @BindView(R2.id.tv_pay_status_desc)
    AppCompatTextView payStatusDesc = null;

    @BindView(R2.id.tv_pay_status_order_no)
    AppCompatTextView payStatusOrderNo = null;

    @BindView(R2.id.tv_pay_status_time)
    AppCompatTextView payStatusTime = null;

    @BindView(R2.id.tv_pay_status_type)
    AppCompatTextView payStatusType = null;

    @BindView(R2.id.tv_pay_status_amount)
    AppCompatTextView payStatusAmount = null;

    private final static String ORDER_NO = "ORDER_NO";

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_pay_status;
    }

    public static PayStatusDelegate create(String orderNo) {
        final Bundle args = new Bundle();
        args.putString(ORDER_NO,orderNo);
        final PayStatusDelegate delegate = new PayStatusDelegate();
        delegate.setArguments(args);
        return delegate;
    }


    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        RestClient.builder()
                .url("alipay/return_url.do")
                .loader(getContext())
                .params("uuid", AccountManager.getSignUUID())
                .params("orderNo", getArguments().getString(ORDER_NO))
                .success(this)
                .build()
                .post();
    }
    @Override
    public void onSuccess(String response) {
        final JSONObject OBJ = JSON.parseObject(response);
        final int code = OBJ.getInteger("code");
        if (OBJ != null && code == 1) {
            final JSONObject obj = OBJ.getJSONObject("data");
            if (obj.getInteger("payStatus") == 1) {
                payStatusImg.setTextColor(Color.parseColor("#338f02"));
                payStatusImg.setText("{icon-pay-status-success}");
                payStatusOrderNo.setText(obj.getString("orderNo"));
                payStatusTime.setText(obj.getString("payDate"));
                payStatusType.setText(obj.getString("payMode"));
            } else {
                payStatusImg.setTextColor(Color.parseColor("#ff0c1c"));
                payStatusImg.setText("{icon-pay-status-failure}");
            }
            payStatusDesc.setText("订单"+obj.getString("payStatusMeaning"));
            payStatusAmount.setText("¥"+obj.getString("realPayAmount"));

        } else {
            Toasty.error(getContext(), "与服务器通信失败!").show();
        }
    }

    @Override
    public boolean onBackPressedSupport() {
        new SuperDialog.Builder(this.getActivity())
                .setRadius(10)
                .setAlpha(0.9f)
                .setWidth(0.6f)
                .setTitle("提示", 40, 80)
                .setMessage("确认退出?", Color.parseColor("#545454"), 40)
                .setPositiveButton("退出", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        LatteLogger.e("click=", "true");
                        getSupportDelegate().pop();
                    }
                })
                .setNegativeButton("取消", new SuperDialog.OnClickNegativeListener() {
                    @Override
                    public void onClick(View v) {
                    }
                })
                .build();
        return true;
    }

}
